using System.Numerics;
using AudioStudio;
using GameFramework.Battle.Core;
using Gameplay.PVE.Data;
using Gameplay.PVE.Utils;
using Yoozoo.Managers;
using Vector3 = UnityEngine.Vector3;

namespace Gameplay.PVE.Entity
{
    public class ObstacleUnit:UnitBase
    {

        private RenderComponent renderComponent;
        private HudComponent hudComponent;
        private bool needDead = false;
        
        public override void Initialize(UnitData data)
        {
            base.Initialize(data);
            needDead = false;
            renderComponent = AddComponent<RenderComponent>();
            AddComponent<ControlComponent>();
            AddComponent<ObstacleBattleComponent>();
            //AddComponent<BattleComponennt>();
            //AddComponent<HudComponent>();
            //AddComponent<ObstacleComponent>();
            AddComponent<HudComponent>();
            if (PveResourceManager.Instance.IsReady)
            {
                //AddComponent<ObstacleHudComponent>();
            }
            OnInitialize();
        }

        public override void SetHudLayer(int layer)
        {
            base.SetHudLayer(layer);
            hudComponent?.SetLayer(layer);
        }

        public override void SetHpChange(float change, bool ignoreShield = false)
        {
            Data.CurrentHp = Data.CurrentHp + change; 
            Transmit(ETransmitType.UpdateHp);
            if (Data.CurrentHp <= 0)
            {
                Dead();
            }
        }
        public override void Dead()
        {
            if (Data.isDead)
            {
                return;
            }
            Transmit(ETransmitType.Dead);
            if (Data.obstacleConfig.dead_effect != 0)
            {
                PveEffectManager.Instance.PlayEffect(Data.obstacleConfig.dead_effect,Data.position,Vector3.forward);
            }

            if (Data.obstacleConfig.shake == 1)
            {
                PveCameraManager.Instance.Shake(0.2f);
            }

            if (!string.IsNullOrEmpty(Data.obstacleConfig.dead_sound))
            {
                AudioManager.PlaySound(Data.obstacleConfig.dead_sound,gameObject);
            }

            var hidingUnit = Data.hidingUnit;
            
            if (Data.deadModelId != 0)
            {
                Data.isDead = true;
                TimeManager.Instance.DelayCall(400, () =>
                {
                    renderComponent.ChangeToDeadModel();
                });
            }
            else
            {
                base.Dead();
                this.gameObject.SetActive(false);
            }
            
            
            if (hidingUnit != null)
            {
                hidingUnit.Data.hidePosition = Vector3.zero;
                hidingUnit.Data.isGoingToHidePoint = false;
                //hidingUnit.Data.isHiding = false;
                //Data.hidingUnit.Transmit(ETransmitType.SquatUp);
                hidingUnit.Transmit(ETransmitType.ObstacleUpdate);
            }

            Data.isDying = false;
        }
    }
}